//
//  TableShowCollectionViewCellModel.swift
//  jhf
//
//  Created by 计海峰 on 2021/12/8.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import RxSwift
import RxDataSources

// MARK: - cell model
/// 用来作为table cell 绑定 cvc 使用
/// table cell  的 bind to 是 该类的 子类
public class TableShowCollectionViewCellModel : NSObject, PublicTableCellConfigProtocol, BindKeyProtocol, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
    
    ///  block  回调使用 做为多次回调跟单次回调
    public typealias TableShowCollectionCellBlock = (UICollectionView, UITableViewCell, TableShowCollectionViewCellModel) -> Void
    
    /// CollectionViewBaseVM cvc model < collection 控制器 >
    public var model: Any?
    
    /// margin 外边距
    public var margin: UIEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
    
    ///  cell 绑定回调  多次调用（ 自定义使用）
    public var bindBlock: TableShowCollectionCellBlock?
    ///  cell 绑定回调  单次次调用（ 自定义使用）
    public var configBlock: TableShowCollectionCellBlock?
    
    /// 绑定 销毁器
    private let bag = DisposeBag()
    
    ///  init 的 model 是 CollectionViewBaseVM cvc model < collection 控制器 >
    required public init(_ model: Any?) {
        self.model = model
    }
    
    // MARK: - 返回指定的cell
    public func cellCls() -> UITableViewCell.Type {
        return TableShowCollectionViewCell.self
    }
    
    // MARK: - tablecell 配置
    
    public typealias TargetType = UICollectionView
    
    // 单次执行 配置一些啥啥的
    public func once(_ collectionV: UICollectionView, cell: UITableViewCell) {
        guard let collectVM = model as? CollectionViewBaseVM else { return }
        collectVM.sectionsSubject.debounce(.milliseconds(100), scheduler: MainScheduler.instance).bind(to: collectionV.rx.items(dataSource: collectVM.configDataSource())).disposed(by: bag)
        collectionV.delegate = self
        collectionV.isUserInteractionEnabled = true
        if collectVM.isLongPressGesture {
            collectionV.addGestureRecognizer(collectVM.panGes)
        }
        if let layout = collectVM.flowLayout {
            collectionV.collectionViewLayout = layout
        }
        collectionV.snp.updateConstraints { make in
            make.edges.equalToSuperview().inset(margin)
        }
        if let block = self.configBlock {
            block(collectionV, cell, self)
        }
    }
    
    // 多次执行
    public func every(_ collectionV: UICollectionView, cell: UITableViewCell) {
        if let block = self.bindBlock {
            block(collectionV, cell, self)
        }
    }

    // MARK: -  UICollectionViewDelegateFlowLayout
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        guard let collectVM = model as? CollectionViewBaseVM else { return CGSize(0.01, 0.01) }
        let section = collectVM.sections[indexPath.section]
        let row = section.items[indexPath.row]
        return row.cellSize
    }
    
    // MARK: - UICollectionViewDelegate
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let collectVM = model as? CollectionViewBaseVM else { return }
        let section = collectVM.sections[indexPath.section]
        let row = section.items[indexPath.row]
        collectVM.showImages(row)
    }
    
}
